package com.copa.gameStore.application.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copa.gameStore.application.GameGoodsApplicationService;
import com.copa.gameStore.domain.GameGoods;
import com.copa.gameStore.dto.GameGoodsDto;
import com.copa.gameStore.mapper.GameGoodsMapper;
import com.copa.gameStore.service.GameGoodsService;
import com.copa.gameStore.vo.ResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-09-26 18:18
 * @function
 */
@Service
public class GameGoodsApplicationServiceImpl implements GameGoodsApplicationService {

    @Resource
    private GameGoodsService gameGoodsService;

    @Resource
    private GameGoodsMapper gameGoodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> add(GameGoodsDto gameGoodsDto) {
        gameGoodsService.add(gameGoodsDto);
        return ResponseVo.successMsg("添加商品成功~");
    }

    @Override
    public ResponseVo<List<GameGoods>> get(String name) {
        return ResponseVo.success(gameGoodsMapper.selectList(new QueryWrapper<GameGoods>()
                .like("name", name)
                .eq("is_enabled", true)
                .orderByDesc("id")
                .select("name", "price", "main_pic_url", "num", "id")));
    }

    @Override
    public ResponseVo<GameGoods> getDetail(Long id) {
        return ResponseVo.success(gameGoodsMapper.selectOne(new QueryWrapper<GameGoods>()
                .eq("id", id)
                .eq("is_enabled", true)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> modify(GameGoodsDto gameGoodsDto) {
        gameGoodsService.modify(gameGoodsDto);
        return ResponseVo.successMsg("修改商品成功~");
    }

    @Override
    public ResponseVo<List<GameGoods>> getAdmin(String name) {
        return ResponseVo.success(gameGoodsMapper.selectList(new QueryWrapper<GameGoods>()
                .like("name", name)
                .orderByDesc("id")
                .select("name", "price", "main_pic_url", "num", "id", "is_enabled")));
    }

    @Override
    public ResponseVo<GameGoods> getDetailAdmin(Long id) {
        return ResponseVo.success(gameGoodsMapper.selectById(id));
    }

    @Override
    public GameGoods getById(Long id) {
        return gameGoodsMapper.selectOne(new QueryWrapper<GameGoods>()
                .eq("id", id)
                .select("name", "price", "main_pic_url", "num", "id"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean validateGoods(Long id) {
        return gameGoodsService.validateGoods(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stock(Long id, Integer num) {
        gameGoodsService.stock(id, num);
    }
}
